package spikes

import (
	"context"
	"gitee.com/enjoy_5/spikes-rpc/internal/server"
	pb "gitee.com/enjoy_5/spikes-rpc/proto/spikes"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// 秒杀服务
type SpikesServer struct {
	pb.UnimplementedSpikesServerServer
}

// 创建秒杀活动
func (u *SpikesServer) CreateSpikesActivity(_ context.Context, req *pb.CreateSpikesActivityRequest) (resp *pb.CreateSpikesActivityResponse, err error) {
	if req.UserID == 0 {
		return nil, status.Errorf(codes.NotFound, "用户id不能为空")
	}

	spikes, err := server.CreateSpikesActivity(req)
	if err != nil {
		return nil, status.Errorf(codes.NotFound, "活动创建失败::%s", err)
	}
	return &pb.CreateSpikesActivityResponse{SpikesActivityID: int64(spikes.ID)}, nil
}

// 获取秒杀活动列表
func (u *SpikesServer) GetSpikesActivityList(_ context.Context, req *pb.GetSpikesActivityListRequest) (resp *pb.GetSpikesActivityListResponse, err error) {
	activity, err := server.GetSpikesActivityList()
	if err != nil {
		return nil, status.Errorf(codes.Unimplemented, "活动列表错误信息::%s", err)
	}
	return &pb.GetSpikesActivityListResponse{SpikesActivityList: activity}, nil
}

// 获取秒杀商品列表
func (u *SpikesServer) GetSpikesGoodsList(_ context.Context, req *pb.GetSpikesGoodsListRequest) (resp *pb.GetSpikesGoodsListResponse, err error) {
	if req.SpikesActivityID <= 0 {
		return nil, status.Errorf(codes.NotFound, "秒杀活动id不能为空")
	}

	list, err := server.GetSpikesGoodsList(req)
	if err != nil {
		return nil, status.Errorf(codes.NotFound, "活动商品列表获取失败::%s", err)
	}
	return &pb.GetSpikesGoodsListResponse{SpikesList: list}, nil
}

// 创建秒杀商品sku关联活动属方法
func (u *SpikesServer) CreateSpikesGoodsSku(_ context.Context, req *pb.CreateSpikesGoodsSkuRequest) (resp *pb.CreateSpikesGoodsSkuResponse, err error) {
	if req.UserID == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "用户id不能为空")
	}
	if req.SpikesActivityID == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "秒杀活动id不能为空")
	}
	if req.GoodsSkuID == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品skuid不能为空")
	}

	sku, err := server.CreateSpikesGoodsSku(req)
	if err != nil || sku == nil {
		return nil, status.Errorf(codes.NotFound, "创建秒杀商品sku关联活动失败::%s", err)
	}
	return &pb.CreateSpikesGoodsSkuResponse{SpikesGoodsSkuID: int64(sku.ID)}, nil
}

// 修改秒杀库存
func (u *SpikesServer) UpdateSpikesStock(_ context.Context, req *pb.UpdateSpikesStockByGoodsIDRequest) (resp *pb.UpdateSpikesStockByGoodsIDResponse, err error) {
	if req.SpikesActivityID == 0 {
		return nil, status.Errorf(codes.NotFound, "秒杀活动id不能为空")
	}
	err = server.UpdateGoodsStock(req)
	if err != nil {
		return nil, status.Errorf(codes.InvalidArgument, "扣减库存失败：：%v", err)
	}
	return nil, nil
}

// 修改秒杀库存回滚
func (u *SpikesServer) RoUpdateSpikesStock(_ context.Context, req *pb.UpdateSpikesStockByGoodsIDRequest) (resp *pb.UpdateSpikesStockByGoodsIDResponse, err error) {
	if req.SpikesActivityID == 0 {
		return nil, status.Errorf(codes.NotFound, "秒杀活动id不能为空")
	}
	err = server.RoUpdateGoodsStock(req)
	if err != nil {
		return nil, status.Errorf(codes.InvalidArgument, "回滚库存失败：：%v", err)
	}
	return nil, nil
}

// 查询秒杀商品详情
func (u *SpikesServer) GetSpikesGoodsDetail(_ context.Context, req *pb.GetSpikesGoodsDetailRequest) (resp *pb.GetSpikesGoodsDetailResponse, err error) {
	info, err := server.GetSpikesGoodsDetail(req)
	if err != nil {
		return nil, status.Errorf(codes.Unimplemented, "查询秒杀详情失败：：%v", err)
	}
	return &pb.GetSpikesGoodsDetailResponse{In: info}, nil
}
